#!/usr/bin/env python3
import requests
import streamlit as st

icons = {
    "router": "img/host.png",
    "switch": "img/switch.png",
    "host": "img/host.png",
}

def fetch_url(url, timeout=5):
    """Fetches data from the given URL."""
    try:
        response = requests.get(url, timeout=timeout)  # Set the timeout
        response.raise_for_status()  # Raise an error for bad responses (4xx and 5xx)
        return response.json()
    except requests.exceptions.Timeout:
        raise requests.exceptions.Timeout(f"Request timed out after {timeout} seconds.")
        # raise f"Exception: Request timed out after {timeout} seconds."
    except requests.exceptions.RequestException as e:
        # raise f"Exception: Failed to fetch data: {e}"
        raise requests.exceptions.RequestException(f"Failed to fetch data: {e}")


def st_get_api(api_end_point):
    API_ADDRESS = st.session_state.address
    url = f'http://{API_ADDRESS}/' + api_end_point
    time_out = st.session_state.timeout
    return url, time_out
